package com.haitang.erp.service.purchase.impl;

import com.haitang.common.core.utils.MapstructUtils;
import com.haitang.common.core.utils.StringUtils;
import com.haitang.common.mybatis.core.page.TableDataInfo;
import com.haitang.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.haitang.erp.domain.purchase.ErpPurchaseIn;
import com.haitang.erp.domain.purchase.bo.ErpPurchaseInBo;
import com.haitang.erp.domain.purchase.vo.ErpPurchaseInVo;
import com.haitang.erp.mapper.purchase.ErpPurchaseInMapper;
import com.haitang.erp.service.purchase.IErpPurchaseInService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * ERP 采购入库Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-21
 */
@RequiredArgsConstructor
@Service
public class ErpPurchaseInServiceImpl implements IErpPurchaseInService {

    private final ErpPurchaseInMapper baseMapper;

    /**
     * 查询ERP 采购入库
     */
    @Override
    public ErpPurchaseInVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ERP 采购入库列表
     */
    @Override
    public TableDataInfo<ErpPurchaseInVo> queryPageList(ErpPurchaseInBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpPurchaseIn> lqw = buildQueryWrapper(bo);
        Page<ErpPurchaseInVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ERP 采购入库列表
     */
    @Override
    public List<ErpPurchaseInVo> queryList(ErpPurchaseInBo bo) {
        LambdaQueryWrapper<ErpPurchaseIn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpPurchaseIn> buildQueryWrapper(ErpPurchaseInBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpPurchaseIn> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNo()), ErpPurchaseIn::getNo, bo.getNo());
        lqw.eq(bo.getStatus() != null, ErpPurchaseIn::getStatus, bo.getStatus());
        lqw.eq(bo.getSupplierId() != null, ErpPurchaseIn::getSupplierId, bo.getSupplierId());
        lqw.eq(bo.getAccountId() != null, ErpPurchaseIn::getAccountId, bo.getAccountId());
        lqw.eq(bo.getInTime() != null, ErpPurchaseIn::getInTime, bo.getInTime());
        lqw.eq(bo.getOrderId() != null, ErpPurchaseIn::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), ErpPurchaseIn::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getTotalCount() != null, ErpPurchaseIn::getTotalCount, bo.getTotalCount());
        lqw.eq(bo.getTotalPrice() != null, ErpPurchaseIn::getTotalPrice, bo.getTotalPrice());
        lqw.eq(bo.getPaymentPrice() != null, ErpPurchaseIn::getPaymentPrice, bo.getPaymentPrice());
        lqw.eq(bo.getTotalProductPrice() != null, ErpPurchaseIn::getTotalProductPrice, bo.getTotalProductPrice());
        lqw.eq(bo.getTotalTaxPrice() != null, ErpPurchaseIn::getTotalTaxPrice, bo.getTotalTaxPrice());
        lqw.eq(bo.getDiscountPercent() != null, ErpPurchaseIn::getDiscountPercent, bo.getDiscountPercent());
        lqw.eq(bo.getDiscountPrice() != null, ErpPurchaseIn::getDiscountPrice, bo.getDiscountPrice());
        lqw.eq(bo.getOtherPrice() != null, ErpPurchaseIn::getOtherPrice, bo.getOtherPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getFileUrl()), ErpPurchaseIn::getFileUrl, bo.getFileUrl());
        return lqw;
    }

    /**
     * 新增ERP 采购入库
     */
    @Override
    public Boolean insertByBo(ErpPurchaseInBo bo) {
        ErpPurchaseIn add = MapstructUtils.convert(bo, ErpPurchaseIn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改ERP 采购入库
     */
    @Override
    public Boolean updateByBo(ErpPurchaseInBo bo) {
        ErpPurchaseIn update = MapstructUtils.convert(bo, ErpPurchaseIn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpPurchaseIn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除ERP 采购入库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
